package cn.stylefeng.guns.modular.wordstudy.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.wordstudy.entity.Sentence;
import cn.stylefeng.guns.modular.wordstudy.entity.Trans;
import cn.stylefeng.guns.modular.wordstudy.entity.Word;
import cn.stylefeng.guns.modular.wordstudy.mapper.WordMapper;
import cn.stylefeng.guns.modular.wordstudy.model.params.WordParam;
import cn.stylefeng.guns.modular.wordstudy.model.result.WordResult;
import cn.stylefeng.guns.modular.wordstudy.service.SentenceService;
import cn.stylefeng.guns.modular.wordstudy.service.TransService;
import cn.stylefeng.guns.modular.wordstudy.service.WordService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 单词信息 服务实现类
 * </p>
 *
 * @author
 * @since 2020-04-28
 */
@Service
public class WordServiceImpl extends ServiceImpl<WordMapper, Word> implements WordService {
    @Autowired
    private TransService transService;
    @Autowired
    private SentenceService sentenceService;

    @Override
    public void add(WordParam param) {
        Word entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(WordParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(WordParam param) {
        Word oldEntity = getOldEntity(param);
        Word newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public WordResult findBySpec(WordParam param) {
        return null;
    }

    @Override
    public List<WordResult> findListBySpec(WordParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(WordParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);

        List<WordResult> records = page.getRecords();
        for (WordResult wordResult : records) {
            List<Trans> transList = transService.list(new QueryWrapper<Trans>().eq("word_id", wordResult.getId()));
            StringBuffer sb = new StringBuffer();
            transList.forEach(trans -> {
                sb.append(trans.getTrans());
            });
            wordResult.setNameCn(sb.toString());

            List<Sentence> sentenceList = sentenceService.list(new QueryWrapper<Sentence>().eq("word_id", wordResult.getId()));
            StringBuffer sb1 = new StringBuffer();
            sentenceList.forEach(trans -> {
                sb1.append(trans.getContentCn() + " " + trans.getContentEn());
            });
            wordResult.setDetail(sb1.toString());
        }
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(WordParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Word getOldEntity(WordParam param) {
        return this.getById(getKey(param));
    }

    private Word getEntity(WordParam param) {
        Word entity = new Word();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
